<template>
  <div class="container">
    <el-card>
      <!-- 输入框 -->
      <el-row :gutter="24" class="header-search">
        <!-- 标签名字 -->
        <el-col :span="4">
          <span>标签名称：</span>
          <el-input
            clearable
            v-model="query.tags"
            placeholder="请输入"
            class="tag"
          ></el-input>
        </el-col>
        <!-- 省 -->
        <el-col :span="3">
          <span>省：</span>
          <el-select
            v-model="query.province"
            placeholder="请选择"
            @change="provinceChange($event)"
            class="tag"
          >
            <el-option
              v-for="(item, index) in province"
              :key="index"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-col>
        <!-- 市 -->
        <el-col :span="3">
          <span>市：</span>
          <el-select v-model="query.city" placeholder="请选择" class="tag">
            <el-option
              v-for="(item, index) in cities"
              :key="index"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-col>
        <!-- 企业简称 -->
        <el-col :span="4">
          <span>企业简称</span>
          <el-input
            v-model="query.shortName"
            placeholder="请输入"
            class="tag"
          ></el-input>
        </el-col>
        <!-- 状态 -->
        <el-col :span="2">
          <span>状态</span>
          <el-select v-model="query.state" placeholder="请选择" class="tag">
            <el-option
              v-for="item in state"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="2">
          <span>操作</span>
          <el-button
            class="tag"
            type="primary"
            icon="el-icon-search"
            @click="searchData"
            >搜索</el-button
          >
        </el-col>
      </el-row>
      <!-- 新增用户 -->
      <el-row class="addUser">
        <el-button
          type="primary"
          icon="el-icon-edit"
          @click="dialogVisible = true"
          >新增用户</el-button
        >
      </el-row>
      <!-- 警告 -->
      <!-- :title="`数据一共${total}条`" -->
      <el-alert
        :title="`数据一共${total}条`"
        type="info"
        show-icon
        :closable="false"
      >
      </el-alert>
      <!-- 表格 -->
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column type="index" label="序号"> </el-table-column>
        <el-table-column prop="number" label="企业编号" width="300">
        </el-table-column>
        <el-table-column
          prop="shortName"
          label="企业简称"
          width="150"
        ></el-table-column>
        <el-table-column prop="tags" label="标签" width="100">
        </el-table-column>
        <el-table-column prop="creatorID" label="创建者" width="100">
        </el-table-column>
        <el-table-column label="创建日期">
          <template slot-scope="scope">
            {{ scope.row.addDate | parseTimeByString }}
          </template>
        </el-table-column>
        <el-table-column prop="remarks" label="备注"> </el-table-column>
        <el-table-column prop="state" label="状态" width="100">
          <template slot-scope="scope">
            {{ scope.row.state == 1 ? '启用' : '禁用' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="250" align="center">
          <template slot-scope="scope">
            <el-button
              @click="handleClick(scope.row)"
              type="primary"
              size="small"
              >编辑</el-button
            >
            <el-button
              type="primary"
              size="small"
              @click="bearButton(scope.row)"
            >
              {{ scope.row.state == 1 ? '禁用' : '启用' }}
            </el-button>
            <el-button
              type="danger"
              size="small"
              @click="deleteCompanys(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="query.page"
          :page-sizes="[5, 10, 20, 40]"
          background
          :page-size="query.pagesize"
          layout=" sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
      <!-- 创建用户弹框 -->
      <!--新增学科 弹框 -->
      <el-dialog
        @close="addDialogClose"
        title="新增课件"
        :visible.sync="dialogVisible"
        width="50%"
        class="dilogHight"
      >
        <!-- 表单 -->
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="150px"
          class="demo-ruleForm"
        >
          <el-form-item label="企业名称" prop="shortName">
            <el-col :span="20">
              <el-input
                v-model="ruleForm.shortName"
                style="width: 90"
              ></el-input>
            </el-col>
            <!-- 单选框 -->
          </el-form-item>
          <el-form-item
            ><el-checkbox v-model="ruleForm.isFamous" name="type"
              >是否为企业</el-checkbox
            ></el-form-item
          >

          <!-- 所属公司 -->
          <el-form-item label="所属公司">
            <el-col :span="20">
              <el-input v-model="ruleForm.company" style="width: 90"></el-input>
            </el-col>
          </el-form-item>
          <!-- 网址 -->
          <el-form-item>
            <span>http//www.tayliyychan.com</span>
          </el-form-item>
          <!-- 下拉省选择框 -->
          <el-form-item required label="城市">
            <el-select
              v-model="ruleForm.province"
              placeholder="请选择"
              @change="provinceChange($event)"
              style="width: 60"
            >
              <el-option
                v-for="(item, index) in province"
                :key="index"
                :label="item"
                :value="item"
              >
              </el-option>
            </el-select>
            <!-- 下拉城市选择框 -->
            <el-select v-model="ruleForm.city" placeholder="请选择">
              <el-option
                v-for="(item, index) in cities"
                :key="index"
                :label="item"
                :value="item"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <!--方向标签  -->
          <el-form-item label="方向（企业标签）" prop="tags">
            <el-col :span="20">
              <el-input v-model="ruleForm.tags"></el-input>
            </el-col>
          </el-form-item>
          <!-- 备注 -->
          <el-form-item label="备注">
            <el-col :span="20">
              <el-input
                type="textarea"
                v-model="ruleForm.remarks"
                style="width: 90"
                placeholder="请输入内容"
              ></el-input>
            </el-col>
          </el-form-item>
          <!-- 按钮 -->
          <el-form-item style="margin: 50px 140px">
            <el-button type="primary" @click="dialogVisible = false"
              >取消</el-button
            >
            <el-button @click="submitForm()">确定</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { list, add, remove, update, disabled } from '@/api/hmmm/companys.js'

//  导入city
import { provinces, citys } from '@/api/hmmm/citys.js'
import { status } from '@/api/hmmm/constants'
export default {
  name: 'companys',
  data() {
    return {
      //  选择框
      query: {
        tags: '', //标签名字
        province: '', //企业所在省份
        city: '', //企业所在城市
        shortName: '', //企业简称
        state: null, //状态
        page: 1, //当前页数
        pagesize: 10, //当前页尺寸
      },
      // 当前页数
      handleChange: {
        page: 0, //当前页数
        pagesize: 0, //当前页尺寸
      },
      // 总条数
      total: 0,
      state: status,
      cities: [], //城市选择器

      province: provinces(), //省份选择器
      //   表格区域
      tableData: [],
      //  弹框控制
      dialogVisible: false,
      //控制新增用户弹框开启
      ruleForm: {
        tags: '', //标签
        city: '', //城市
        company: '', //所属公司
        province: '', //所属省份
        isFamous: '', //是否名企
        remarks: '', //备注
        shortName: '', //企业名称
        id: '',
        number: '',
        province: '',
      },
      rules: {
        shortName: [
          { required: true, message: 'data', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' },
        ],
        direct: [
          { required: true, message: 'direct', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' },
        ],
      },
      // 按钮状态
      disabledBttuon: {
        id: '', //数据
        state: 1, //状态
      },
    }
  },
  async created() {
    this.getCanpanyList()
    // this.province = await provinces()
  },
  methods: {
    addDialogClose() {
      this.$refs.ruleForm.resetFields()
      this.tags = ''
      this.ruleForm.remarks = ''
      this.ruleForm.company = ''
      this.ruleForm.province = ''
      this.ruleForm.city = ''
      this.ruleForm.isFamous = false
    },
    //   获取表格数据
    async getCanpanyList() {
      try {
        const { data } = await list(this.query)
        this.tableData = data.items //把数据给table
        this.total = data.counts
      } catch (err) {
        this.$message.error('获取数据失败')
      }
    },

    //  查看区域
    async provinceChange(val) {
      this.cities = await citys(val)
    },

    //  搜索功能
    async searchData() {
      try {
        this.getCanpanyList()
        this.$message.success('成功')
      } catch (err) {
        console.log(err)
      }
    },
    //   分页区域
    handleSizeChange(val) {
      //  更新数据
      this.query.pagesize = val
      this.getCanpanyList()
    },
    handleCurrentChange(val) {
      // 更新数据
      this.query.page = val
      this.getCanpanyList()
    },
    //  新增用户弹框
    submitForm() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          try {
            if (!this.ruleForm.id) {
              await add(this.ruleForm)
            } else {
              await update(this.ruleForm)
            }
            this.$message.success('更新数据成功')
            this.getCanpanyList() //更新数据
            this.dialogVisible = false //关闭弹框
          } catch (err) {
            console.log(err)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    //  删除功能
    deleteCompanys(value) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          await remove(value)
          this.$message({
            type: 'success',
            message: '删除成功!',
          })
          this.getCanpanyList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    //  禁用功能
    async bearButton(val) {
      // console.log(this.tableData.state);
      //  this.disabledBttuon.state=!val.state
      //  console.log(this.disabledBttuon.state);
      // this.disabledBttuon.state=val.state == 1? '0':'1'
      try {
        await disabled({
          id: val.id,
          state: val.state == 1 ? '0' : '1',
        })
        this.getCanpanyList()
      } catch (err) {
        console.log(err)
      }
    },

    // 编辑功能
    handleClick(value) {
      console.log(value)
      this.dialogVisible = true
      //  数据更新
      this.ruleForm.tags = value.tags
      this.ruleForm.city = value.city
      this.ruleForm.company = value.company
      this.ruleForm.province = value.province
      this.ruleForm.isFamous = value.isFamous == 1 ? true : false
      this.ruleForm.remarks = value.remarks
      this.ruleForm.shortName = value.shortName
      this.ruleForm.id = value.id
      this.ruleForm.number = value.number
      this.ruleForm.province = value.province
      // 渲染页面
      this.getCanpanyList()
    },
  },
}
</script>
<style lang='less'  scoped>
.container {
  .addUser {
    display: flex;
    justify-content: flex-end;
  }
  .block {
   display: flex;
   justify-content: flex-end;
  }
  .el-form-item__content {
    .el-select {
      width: 120px;
    }
  }
  .header-search {
    margin-bottom: 40px;
  }
  .el-alert {
    margin: 10px 0;
  }
  .tag {
    margin-top: 10px;
  }
}
</style>


